package com.tianyuan.lims.utils;

import org.apache.commons.lang3.StringUtils;

import java.util.*;
import java.util.function.Function;

public class AlgoStringJoinUtil {
    /**
     * 不重复的合并内容
     *
     * @param entities
     * @param toStrFun
     * @param <T>
     * @return null == entities return null ; entities.size() == 0 return ""
     *
     */
    public static <T> String joinNoBlankAndRepeat(Collection<T> entities, Function<T, String> toStrFun, String separator) {
        if( null == entities ){
            return null;
        }

        if( entities.size() == 0 ){
            return "";
        }

        Set<String> m = new LinkedHashSet<>();
        for (T t : entities) {
            String s = toStrFun.apply(t);
            if ( AlgoStringUtils.isNotBlank(s) && !m.contains(s)) {
                m.add(s);
            }
        }
        StringJoiner stringJoiner = new StringJoiner(separator);
        m.forEach((s) -> {
            stringJoiner.add(s);
        });
        return stringJoiner.toString();
    }

    /**
     * 不重复的合并内容
     *
     * @param entities
     * @param toKeyFunction
     * @param toStrFun
     * @param <T>
     * @param <K>
     * @return null == entities return null ; entities.size() == 0 return ""
     *
     */
    public static <T, K> String joinNoBlankAndRepeat(Collection<T> entities, Function<T, K> toKeyFunction, Function<T, String> toStrFun, String separator) {
        if( null == entities ){
            return null;
        }

        if( entities.size() == 0 ){
            return "";
        }

        Map<K, String> m = new LinkedHashMap<>();
        for (T t : entities) {
            K k = toKeyFunction.apply(t);
            String s = toStrFun.apply(t);
            if ( AlgoStringUtils.isNotBlank(s) && !m.containsKey(k)) {
                m.put(k, s);
            }
        }
        StringJoiner stringJoiner = new StringJoiner(separator);
        m.forEach((k, v) -> {
            stringJoiner.add(v);
        });
        return stringJoiner.toString();
    }

    public static String joinStringNoBlankAndRepeat(Collection<String> entities, String separator) {
        return joinNoBlankAndRepeat(entities, s->s, s->s, separator);
    }

    public static String joinStringNoBlank(Collection<String> entities, String separator) {
        StringJoiner stringJoiner = new StringJoiner(separator);
        for ( String s : entities ){
            if(StringUtils.isNotBlank(s) ){
                stringJoiner.add(s);
            }
        }
        return stringJoiner.toString();
    }

    public static <T> String joinNoBlankSimple(Collection<T> entities, Function<T, String> toStrFun, String separator) {
        StringJoiner stringJoiner = new StringJoiner(separator);
        for ( T t : entities ){
            String s = toStrFun.apply(t);
            if(StringUtils.isNotBlank(s) ){
                stringJoiner.add(s);
            }
        }
        return stringJoiner.toString();
    }

    public static <T> String joinNoBlankAndRepeatSimple(Collection<T> entities, Function<T, String> toStrFun, String separator) {
        StringJoiner stringJoiner = new StringJoiner(separator);
        Set<String> strSet = new HashSet<>();
        for ( T t : entities ){
            String s = toStrFun.apply(t);
            if( StringUtils.isNotBlank(s) && !strSet.contains(s) ){
                stringJoiner.add(s);
                strSet.add(s);
            }
        }
        return stringJoiner.toString();
    }
}
